﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace mVu
{
  class RedTube : Site
  {
    public override Dictionary<string, string> GetCategories()
    {
      var categories = new Dictionary<string, string>
                         {
                           {String.Empty, "http://www.redtube.com/?avID=8&amp;avEnter=1"}
                         };
      return categories;
    }

    public override Dictionary<string, string> GetSorts()
    {
      var sorts = new Dictionary<string, string>
                    {
                      {String.Empty, String.Empty}
                    };

      return sorts;
    }

    public override Dictionary<string, string> GetMediaTypes()
    {
      var types = new Dictionary<string, string>
                    {
                      {"FLV", "FLV"}
                    };
      return types;
    }

    public override List<Thumbnail> GetThumbnails(string baseAddress, string sort, string query, int page)
    {
      var list = new List<Thumbnail>();

      try
      {
        if (String.IsNullOrEmpty(baseAddress))
        {
          baseAddress = "http://www.redtube.com/?avID=8&amp;avEnter=1";
        }

        var address = String.IsNullOrEmpty(query) ? baseAddress : baseAddress + "&search=" + query;
        var result = GetPageSource(address);

        if (!String.IsNullOrEmpty(result))
        {
          var rh = new SgmlReaderDll.SGMLReaderHelper();
          var xhtml = rh.ProcessString(result);

          var doc = XDocument.Parse(xhtml);
          var xmlns = XNamespace.Get("http://www.w3.org/1999/xhtml");

          var nodes = doc.Descendants(xmlns + "div").Where(n => n.Attribute("class") != null && n.Attribute("class").Value == "video");

          list.AddRange(nodes.Select(node => new Thumbnail
                                               {
                                                 Url = node.Descendants(xmlns + "a").Attributes("href").First().Value,
                                                 VideoID = node.Descendants(xmlns + "a").Attributes("href").First().Value,
                                                 Title = node.Descendants(xmlns + "a").Attributes("title").First().Value,
                                                 Image = node.Descendants(xmlns + "img").Attributes("src").First().Value
                                               }));
        }
      }
      catch (Exception ex)
      {
        Utilities.HandleError(ex);
      }

      return list;
    }

    public override Dictionary<string, string> GetVideoPaths(string url)
    {
      var dictionary = new Dictionary<string, string>();

      try
      {
        if (!String.IsNullOrEmpty(url))
        {
          var result = GetPageSource(url);

          if (!String.IsNullOrEmpty(result))
          {
            var start = result.IndexOf("flashvars");
            start = result.IndexOf("http", start + 1);
            var end = result.IndexOf("\"", start + 1);

            var path = result.Substring(start, end - start);
            path = path.Replace("%3A", ":").Replace("%2F", "/").Replace("%3F", "?").Replace("%3D", "=").Replace("%26", "&");
            dictionary.Add("FLV", path);
          }
        }
      }
      catch (Exception ex)
      {
        Utilities.HandleError(ex);
      }

      return dictionary;
    }
  }
}
